package com.social.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.social.pojo.PageBean;
import com.social.pojo.Result;
import com.social.pojo.SysMenu;
import com.social.service.SysMenuService;
import com.social.dao.SysMenuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static com.social.constant.RedisConstants.ROLE_MENU_KEY;
import static com.social.constant.RedisConstants.ROLE_MENU_TTL;

/**
* @author 25241
* @description 针对表【sys_menu】的数据库操作Service实现
* @createDate 2022-10-30 15:18:42
*/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu>
    implements SysMenuService {

    @Autowired
    private SysMenuDao sysMenuDao;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<SysMenu> buildTreeMenu(List<SysMenu> sysMenuList) {
        List<SysMenu> resultMenuList = new ArrayList<>();

        for (SysMenu sysMenu : sysMenuList) {

            // 寻找子节点
            for (SysMenu e : sysMenuList) {
                if (e.getParentId().equals(sysMenu.getId())) {
                    sysMenu.getChildren().add(e);
                }
            }

            if (sysMenu.getParentId() == 0L) {
                resultMenuList.add(sysMenu);
            }
        }

        return resultMenuList;
    }

    @Override
    public Result findAll(PageBean pageBean) {

        IPage<SysMenu> page = sysMenuDao
                .selectPage(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), null);
        List<SysMenu> records = page.getRecords();
        if (records == null) {
            return Result.fail(201, "获取菜单信息失败");
        }
        Map<String, Object> menuList = new HashMap<>();
        menuList.put("menuList", records);
        menuList.put("total", page.getTotal());
        return Result.ok(200, menuList);
    }

    @Override
    public Result deleteByIds(Long[] ids) {
        boolean isRemove = removeByIds(Arrays.asList(ids));
        if (!isRemove){
            return Result.fail(201, "删除数据失败");
        }
        for (Long id : ids) {
            String key = ROLE_MENU_KEY + id;
            stringRedisTemplate.delete(key);
        }
        return Result.ok(200);
    }

    @Override
    public Result listAll() {
        Map<String, Object> resultMap = new HashMap<>();
        List<SysMenu> menuList = list();
        System.out.println("menuList " + menuList);
        resultMap.put("menuList", menuList);
        return Result.ok(200, resultMap);
    }

    @Override
    public Result add(SysMenu sysMenu) {

        if (sysMenu.getId() == null || sysMenu.getId() == -1){
            sysMenu.setCreateTime(new Date());
            boolean isSave = save(sysMenu);
            if (!isSave){
                return Result.fail(201, "添加菜单失败");
            }
            return Result.ok(200);
        } else {
            sysMenu.setUpdateTime(new Date());
            boolean isUpdate = updateById(sysMenu);
            if (!isUpdate) {
                return Result.fail(201, "修改菜单失败");
            }
            String key = ROLE_MENU_KEY + sysMenu.getId();
            String json = JSONUtil.toJsonStr(sysMenu);
            stringRedisTemplate.opsForValue().set(key, json, ROLE_MENU_TTL, TimeUnit.MINUTES);
            return Result.ok(200);
        }
    }


}




